package collector.listener;

import java.awt.Color;
import java.text.MessageFormat;

import bean.util.WindowUtil;
import br.com.wagnerpaz.javahook.NativeMouseEvent;
import br.com.wagnerpaz.javahook.NativeMouseListener;
import br.com.wagnerpaz.javahook.NativeMouseWheelEvent;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;

public class CollectorMouseListener implements NativeMouseListener {

	private HWND window = null;

	public CollectorMouseListener(HWND window) {
		this.window = window;
	}

	public boolean mousePressed(NativeMouseEvent e) {
		System.out.println(getRelaRect("P", e.getX(), e.getY()));
		return true;
	}

	public boolean mouseReleased(NativeMouseEvent e) {
		System.out.println(getRelaRect("R", e.getX(), e.getY()));
		return true;
	}

	public boolean mouseMoved(NativeMouseEvent e) {
		System.out.print(getRelaRect("M", e.getX(), e.getY()));
		System.out.println("  " + getColor(e.getX(), e.getY()));
		return true;
	}

	public boolean mouseWheelMoved(NativeMouseWheelEvent e) {
		System.out.println(getRelaRect("W", e.getX(), e.getY()));
		return true;
	}

	private String getRelaRect(String action, int x, int y) {
		RECT rect = new RECT();
		User32.INSTANCE.GetWindowRect(window, rect);

		int relaX = x - rect.left;
		int relaY = y - rect.top;

		return MessageFormat.format(action + "[{0}, {1}]", relaX, relaY);
	}

	private String getColor(int x, int y) {
		Color color = WindowUtil.robot.getPixelColor(x, y);

		return MessageFormat.format("({0}, {1}, {2})  [{3}]", color.getRed(),
				color.getGreen(), color.getBlue(), color.getRGB());
	}

}
